/*
 *  파일 변경 전 백업파일 저장 -  수정파일이름_backup.txt
 *  파일 변경 후 변경된 파일 저장 -  수정파일이름.txt
 *  파일 변경 내용을 저장하여 기록 -  파일수정정보.txt
 *  세 파일을 압축하여  (년월일-시분)-이름-수정파일이름.zip로 남기세요
 */

/* 	파일 내용을 변경하고 커밋을 하기 전에 modifier에 이름 추가
 * 
 *  ( 년 월 일 시 분 ) - 이름 - 수정 부분(간략히)
 *  ex) 20141228_1342 - 김영우 - 스레드 컨트롤
 *  
 *  ** 기록은 3명까지 남기세요 **
 */

/* 
 *  MODIFIER
 *  
 *  20141228_0218 - 김영우 - onOff() / ChattingThread() / chatView()
 */

package server;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridLayout;

import javax.swing.JScrollPane;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JList;
import javax.swing.JButton;

import java.awt.Color;

import javax.swing.JTextPane;

import main.Login;
import voSetting.SetMyInfoVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Vector;

public class Server extends JFrame{
	
	boolean onOff = false;		// 서버 on,off 상태
	boolean Threadstate = false;
	boolean chatView = false;
	
	Login login_f ;
	ServerSocket server;
	ServerThread serverthread;
	Thread onOff_server;
	boolean accreeControl = true;
	
	private int port_number = 50000;
	
	private Vector<IntermediateServer> chat_intermediate_controller = new Vector<IntermediateServer>();
	private Vector<IntermediateServer> file_intermediate_controller = new Vector<IntermediateServer>();
	
	private HashMap<String, ServerThread> clients = new HashMap<String, ServerThread>();
	
	protected HashMap<String, SetMyInfoVO> user_information = new HashMap<String, SetMyInfoVO>();
	protected DefaultListModel<String> user_list = new DefaultListModel<String>();
	
	protected DefaultListModel<String> room_info = new DefaultListModel<String>();
	
	private JPanel contentPane;
	private JPanel panel;
	private JScrollPane scrollPane;
	private JList list;
	private JLabel lblConnectors;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JPanel panel_4;
	private JPanel panel_5;
	private JPanel panel_6;
	private JLabel label_1;
	private JLabel lbState;
	private JButton lbOnOff;
	private JPanel panel_7;
	private JPanel panel_8;
	private JLabel lblMinoServer;
	private JPanel panel_9;
	private JButton btnThreadState;
	private JButton btnChattingView;
	private JScrollPane scrollPane_1;
	private JLabel lblWholeProcess;
	private JPanel panel_10;
	private JScrollPane scroll_chatThread;
	private JScrollPane scroll_fileThread;
	private JList list_1;
	private JList list_2;
	private JLabel lblChattingTransferState;
	private JLabel lblFileTransferState;
	private JScrollPane scrollPane_6;
	private JList list_3;
	private JLabel lblUserInformation;
	private JList list_4;
	private JPanel panel_11;
	private JScrollPane scrollPane_2;
	private JTextPane textPane;
	private JLabel lblChattingView;
	private JPanel panel_12;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JLabel ipaddress;
	private JButton lbLogin;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Server frame = new Server();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void onOff(){
		if(onOff){
			int Choice = JOptionPane.showConfirmDialog(Server.this, "서버를 종료하시겠습니까?", "서버종료", 2);
			if(Choice==JOptionPane.OK_OPTION){
				JOptionPane.showMessageDialog(Server.this,"서버를 종료합니다.", "서버 종료", 1);	
				lbState.setText("서버가 종료되었습니다..");
				lbOnOff.setText("ON");
				
				accreeControl = false;
				onOff_server = null;
				onOff = false;
			}else{
				JOptionPane.showMessageDialog(Server.this,"서버종료를 취소합니다.", "서버 종료 취소", 1);
			}
			
		}else{
			JOptionPane.showMessageDialog(Server.this, "서버를 실행합니다.", "확인", 1);
			lbState.setText("서버가 실행중입니다.");
			lbOnOff.setText("OFF");
			onOff = true;
			onOff_server = new Thread(){
				@Override
				public void run() {
					try {
						server = new ServerSocket(1111);
						while (accreeControl) {
							Socket socket = server.accept();
							InetAddress ia = socket.getInetAddress();
							String ip = ia.getHostAddress();
							serverthread = new ServerThread(Server.this, socket);
						}
						System.out.println(" 서버 접속 스레드 정지");
					} catch (Exception e) {
						
					}
				}
			};
			onOff_server.start();
		}
	}
	
	public void ChattingThread(){
		if(Threadstate){
			panel_10.setVisible(true);
			Threadstate = false;
		}else{
			panel_10.setVisible(false);
			Threadstate = true;
		}
	}
	
	public void chatView(){
		if(chatView){
			panel_11.setVisible(true);
			chatView = false;
		}else{
			panel_11.setVisible(false);
			chatView = true;
		}
	}
	
	public DefaultListModel<String> getRoom_info() {	// 채팅 및 파일 서버의 정보
		return room_info;
	}
	public void resetRoom_info() {
		this.room_info.removeAllElements();;
	}
	public void setRoom_info(String room_info) {
		this.room_info.addElement(room_info);
	}
	//---------------------------------------------------------------------------------------

	
	public DefaultListModel<String> getUser_list() {	 // 접속한 사용자들의 정보
		return user_list;
	}
	public void resetUser_list() {
		this.user_list.removeAllElements();;
	}
	public void setUser_list(String user_list) {
		this.user_list.addElement(user_list);
	}
	//---------------------------------------------------------------------------------------
	
	
	
	public HashMap<String, SetMyInfoVO> getUser_information() {	// 유저정보 리스트 all
		return user_information;
	}
	public void addUser_information(SetMyInfoVO user_information) {	// 유저정보 리스트 add
		this.user_information.put(user_information.getM_id(), user_information);
	}
	public void dropUser_information(String user_id) { 	// 유저정보 리스트 remove
		this.user_information.remove(user_id);
	}
	//---------------------------------------------------------------------------------------
	
	
	
	public Vector<IntermediateServer> getChat_intermediate_controller() {
		return chat_intermediate_controller;
	}
	public void setChat_intermediate_controller(
			Vector<IntermediateServer> chat_intermediate_controller) {
		this.chat_intermediate_controller = chat_intermediate_controller;
	}
	//---------------------------------------------------------------------------------------
	
	
	public Vector<IntermediateServer> getFile_intermediate_controller() {
		return file_intermediate_controller;
	}
	public void setFile_intermediate_controller(
			Vector<IntermediateServer> file_intermediate_controller) {
		this.file_intermediate_controller = file_intermediate_controller;
	}
	//---------------------------------------------------------------------------------------
	
	
	public HashMap<String, ServerThread> getClients() {	// 아이디를 찾기위한 검색용
		return clients;
	}
	public ServerThread getClientServer(String key){	// 저장되어있는 서버를 호출해주는 메소드
		return clients.get(key);
	}
	public void addClients(String key, ServerThread thread) {	// 키와 서버스레드를 저장
		this.clients.put(key, thread);
	}
	//---------------------------------------------------------------------------------------
	/**
	 * Create the frame.
	 */
	public Server() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.CENTER);
		setLocationRelativeTo(null);
		
		try {
			InetAddress ia = InetAddress.getLocalHost();
			ipaddress.setText(ia.getHostAddress());
			
		} catch (Exception e) {
		}
	}
	
	
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(5, 5));
			panel.add(getScrollPane(), BorderLayout.WEST);
			panel.add(getPanel_1(), BorderLayout.NORTH);
			panel.add(getPanel_7(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getList_1());
			scrollPane.setColumnHeaderView(getLblConnectors());
		}
		return scrollPane;
	}
	private JList getList_1() {
		if (list == null) {
			list = new JList(user_list);
		}
		return list;
	}
	private JLabel getLblConnectors() {
		if (lblConnectors == null) {
			lblConnectors = new JLabel("CONNECTORS (전체 접속자)");
			lblConnectors.setHorizontalAlignment(SwingConstants.CENTER);
			lblConnectors.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lblConnectors;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(5, 5));
			panel_1.add(getPanel_2(), BorderLayout.NORTH);
			panel_1.add(getPanel_3(), BorderLayout.SOUTH);
			panel_1.add(getPanel_4_1(), BorderLayout.WEST);
			panel_1.add(getPanel_5(), BorderLayout.EAST);
			panel_1.add(getPanel_6(), BorderLayout.CENTER);
		}
		return panel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(new BorderLayout(5, 5));
			panel_3.add(getPanel_9(), BorderLayout.WEST);
			panel_3.add(getIpaddress(), BorderLayout.EAST);
		}
		return panel_3;
	}
	private JPanel getPanel_4_1() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.add(getLblMinoServer());
		}
		return panel_4;
	}
	private JPanel getPanel_5() {
		if (panel_5 == null) {
			panel_5 = new JPanel();
			panel_5.add(getLabel_1());
			panel_5.add(getLbState());
			panel_5.add(getLbOnOff());
			panel_5.add(getLbLogin());
		}
		return panel_5;
	}
	private JPanel getPanel_6() {
		if (panel_6 == null) {
			panel_6 = new JPanel();
		}
		return panel_6;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("현재 상태 : ");
			label_1.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return label_1;
	}
	private JLabel getLbState() {
		if (lbState == null) {
			lbState = new JLabel("서버가 종료되었습니다.");
			lbState.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lbState;
	}
	private JButton getLbOnOff() {
		if (lbOnOff == null) {
			lbOnOff = new JButton("ON");
			lbOnOff.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					onOff();
				}
			});
			lbOnOff.setVerticalAlignment(SwingConstants.BOTTOM);
			lbOnOff.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lbOnOff;
	}
	private JPanel getPanel_7() {
		if (panel_7 == null) {
			panel_7 = new JPanel();
			panel_7.setLayout(new BorderLayout(5, 5));
			panel_7.add(getPanel_8(), BorderLayout.NORTH);
			panel_7.add(getPanel_10(), BorderLayout.WEST);
			panel_7.add(getScrollPane_6(), BorderLayout.CENTER);
			panel_7.add(getPanel_11_1(), BorderLayout.EAST);
		}
		return panel_7;
	}
	private JPanel getPanel_8() {
		if (panel_8 == null) {
			panel_8 = new JPanel();
			panel_8.setLayout(new BorderLayout(0, 0));
			panel_8.add(getScrollPane_1());
		}
		return panel_8;
	}
	private JLabel getLblMinoServer() {
		if (lblMinoServer == null) {
			lblMinoServer = new JLabel(" MINO SERVER  ");
			lblMinoServer.setFont(new Font("Broadway", Font.BOLD | Font.ITALIC, 20));
		}
		return lblMinoServer;
	}
	private JPanel getPanel_9() {
		if (panel_9 == null) {
			panel_9 = new JPanel();
			panel_9.add(getBtnThreadState());
			panel_9.add(getBtnChattingView());
		}
		return panel_9;
	}
	private JButton getBtnThreadState() {
		if (btnThreadState == null) {
			btnThreadState = new JButton("Thread State");
			btnThreadState.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ChattingThread();
				}
			});
			btnThreadState.setForeground(Color.BLACK);
			btnThreadState.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		}
		return btnThreadState;
	}
	private JButton getBtnChattingView() {
		if (btnChattingView == null) {
			btnChattingView = new JButton("Chatting View");
			btnChattingView.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					chatView();
				}
			});
			btnChattingView.setForeground(Color.BLACK);
			btnChattingView.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		}
		return btnChattingView;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setColumnHeaderView(getLblWholeProcess());
			scrollPane_1.setViewportView(getList_4());
		}
		return scrollPane_1;
	}
	private JLabel getLblWholeProcess() {
		if (lblWholeProcess == null) {
			lblWholeProcess = new JLabel("WHOLE PROCESS (전체 프로세스)");
			lblWholeProcess.setHorizontalAlignment(SwingConstants.CENTER);
			lblWholeProcess.setFont(new Font("맑은 고딕", Font.BOLD, 12));
			lblWholeProcess.setFocusTraversalKeysEnabled(false);
		}
		return lblWholeProcess;
	}
	private JPanel getPanel_10() {
		if (panel_10 == null) {
			panel_10 = new JPanel();
			panel_10.setVisible(false);
			panel_10.setLayout(new GridLayout(0, 1, 0, 0));
			panel_10.add(getScroll_chatThread());
			panel_10.add(getScroll_fileThread());
		}
		return panel_10;
	}
	private JScrollPane getScroll_chatThread() {
		if (scroll_chatThread == null) {
			scroll_chatThread = new JScrollPane();
			scroll_chatThread.setViewportView(getList_1_2());
			scroll_chatThread.setColumnHeaderView(getLblChattingTransferState());
		}
		return scroll_chatThread;
	}
	private JScrollPane getScroll_fileThread() {
		if (scroll_fileThread == null) {
			scroll_fileThread = new JScrollPane();
			scroll_fileThread.setViewportView(getList_2());
			scroll_fileThread.setColumnHeaderView(getLblFileTransferState());
		}
		return scroll_fileThread;
	}
	private JList getList_1_2() {
		if (list_1 == null) {
			list_1 = new JList();
		}
		return list_1;
	}
	private JList getList_2() {
		if (list_2 == null) {
			list_2 = new JList();
		}
		return list_2;
	}
	private JLabel getLblChattingTransferState() {
		if (lblChattingTransferState == null) {
			lblChattingTransferState = new JLabel("CHATTING TRANSFER STATE (채팅 전송 현황)");
			lblChattingTransferState.setHorizontalAlignment(SwingConstants.CENTER);
			lblChattingTransferState.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lblChattingTransferState;
	}
	private JLabel getLblFileTransferState() {
		if (lblFileTransferState == null) {
			lblFileTransferState = new JLabel("FILE TRANSFER STATE (파일 전송 현황)");
			lblFileTransferState.setHorizontalAlignment(SwingConstants.CENTER);
			lblFileTransferState.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lblFileTransferState;
	}
	private JScrollPane getScrollPane_6() {
		if (scrollPane_6 == null) {
			scrollPane_6 = new JScrollPane();
			scrollPane_6.setViewportView(getList_3());
			scrollPane_6.setColumnHeaderView(getLblUserInformation());
		}
		return scrollPane_6;
	}
	private JList getList_3() {
		if (list_3 == null) {
			list_3 = new JList(room_info);
		}
		return list_3;
	}
	private JLabel getLblUserInformation() {
		if (lblUserInformation == null) {
			lblUserInformation = new JLabel("User Information (사용자 정보)");
			lblUserInformation.setHorizontalAlignment(SwingConstants.CENTER);
			lblUserInformation.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lblUserInformation;
	}
	private JList getList_4() {
		if (list_4 == null) {
			list_4 = new JList();
		}
		return list_4;
	}
	private JPanel getPanel_11_1() {
		if (panel_11 == null) {
			panel_11 = new JPanel();
			panel_11.setVisible(false);
			panel_11.setLayout(new BorderLayout(0, 0));
			panel_11.add(getScrollPane_2(), BorderLayout.CENTER);
			panel_11.add(getPanel_12(), BorderLayout.NORTH);
		}
		return panel_11;
	}
	private JScrollPane getScrollPane_2() {
		if (scrollPane_2 == null) {
			scrollPane_2 = new JScrollPane();
			scrollPane_2.setViewportView(getTextPane());
			scrollPane_2.setColumnHeaderView(getLblChattingView());
		}
		return scrollPane_2;
	}
	private JTextPane getTextPane() {
		if (textPane == null) {
			textPane = new JTextPane();
		}
		return textPane;
	}
	private JLabel getLblChattingView() {
		if (lblChattingView == null) {
			lblChattingView = new JLabel("CHATTING VIEW (대화 내용)");
			lblChattingView.setHorizontalAlignment(SwingConstants.CENTER);
			lblChattingView.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lblChattingView;
	}
	
	private JPanel getPanel_12() {
		if (panel_12 == null) {
			panel_12 = new JPanel();
			panel_12.add(getBtnNewButton());
			panel_12.add(getBtnNewButton_1());
		}
		return panel_12;
	}
	
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("LOGOUT");
			btnNewButton.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		}
		return btnNewButton;
	}
	
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("button");
			btnNewButton_1.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		}
		return btnNewButton_1;
	}
	
	private JLabel getIpaddress() {
		if (ipaddress == null) {
			ipaddress = new JLabel("SERVER IP ADDRESS");
			ipaddress.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return ipaddress;
	}
	
	public int getPort_number() {
		return port_number;
	}

	public void setPort_number() {
		this.port_number++;
	}
	private JButton getLbLogin() {
		if (lbLogin == null) {
			lbLogin = new JButton("Login");
			lbLogin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					login_f = new Login();
					login_f.setVisible(true);
				}
			});
			lbLogin.setVerticalAlignment(SwingConstants.TOP);
			lbLogin.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lbLogin;
	}
}
